package com.sgb.goods.widget.pop.adapter;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.CommonStAttrEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.HashMap;
import java.util.List;

/**
 * 描述：商砼属性弹窗数据适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.09
 */
public class StAttributeViewAdapter extends BaseRecycleAdapter<CommonStAttrEntity> {

    private final HashMap<String, List<CommonStAttrEntity.ChildListBean>> attributeMap = new HashMap<>();
    private final HashMap<String, CommonStAttrEntity.ChildListBean> otherAttributeMap = new HashMap<>();

    public StAttributeViewAdapter(Context context, List<CommonStAttrEntity> datas) {
        super(context, R.layout.goods_attribute_item, datas);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void convert(ItemViewHolder holder, final CommonStAttrEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RecyclerView mRecyclerView = holder.getItem().findViewById(R.id.goods_attribute_recycler_view);
        TextView textView = holder.getItem().findViewById(R.id.goods_attribute_name);
        textView.setText(convertAttribute(entity.getName()));
        StAttributeChildAdapter mStAttributeChildAdapter = new StAttributeChildAdapter(context, entity.getChildList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mStAttributeChildAdapter);
        if (TextUtils.equals(entity.getName(), "admixture")) {
            mStAttributeChildAdapter.setMultiSelect(true);
            mStAttributeChildAdapter.setOnItemMultiClick2((childListBeans, position, view) -> {
                if (attributeMap.containsKey(entity.getName())) {
                    attributeMap.replace(entity.getName(), childListBeans);
                } else {
                    attributeMap.put(entity.getName(), childListBeans);
                }
            });
        } else {
            mStAttributeChildAdapter.setItemSelect(true);
            mStAttributeChildAdapter.setOnItemClick(new AdapterOnItemClick<CommonStAttrEntity.ChildListBean>() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onItemClick(CommonStAttrEntity.ChildListBean childListBean, int position) {
                    if (otherAttributeMap.containsKey(entity.getName())) {
                        otherAttributeMap.replace(entity.getName(), childListBean);
                    } else {
                        otherAttributeMap.put(entity.getName(), childListBean);
                    }
                }
            });
        }
    }


    public HashMap<String, List<CommonStAttrEntity.ChildListBean>> getAdmixtureAttrData() {
        return attributeMap;
    }

    public HashMap<String, CommonStAttrEntity.ChildListBean> getOtherAttrData() {
        return otherAttributeMap;
    }

    private String convertAttribute(String attribute) {
        if (TextUtils.equals(attribute, "strength")) {
            return "强度等级";
        } else if (TextUtils.equals(attribute, "admixture")) {
            return "外加剂";
        } else if (TextUtils.equals(attribute, "config")) {
            return "配置";
        } else if (TextUtils.equals(attribute, "deliveryMethod")) {
            return "服务";
        }
        return "";
    }
}